package com.taiyi.renaissancemall.ui.mine.address

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.utils.DensityUtil
import com.liaoinstan.springview.widget.SpringView
import com.taiyi.renaissancemall.R
import com.taiyi.renaissancemall.Utils.MyDecoration
import com.taiyi.renaissancemall.adapter.AdapterManager
import com.taiyi.renaissancemall.adapter.baseadapter.MultiItemTypeAdapter
import com.taiyi.renaissancemall.base.BaseActivity
import com.taiyi.renaissancemall.bean.AddressListBean
import com.taiyi.renaissancemall.ui.mine.address.add.AddAddressActivity
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration
import kotlinx.android.synthetic.main.activity_shipping_address.*
import kotlinx.android.synthetic.main.base_top_black_background_layout.*
import org.greenrobot.eventbus.EventBus

class ShippingAddressActivity : BaseActivity<ShippingAddressView, ShippingAddressPresent>(),
    ShippingAddressView, MultiItemTypeAdapter.OnItemClickListener, SpringView.OnFreshListener {
    lateinit var selectAddressId: String
    var pageNo = 1
    var data = mutableListOf<AddressListBean.AddresslistBean>()

    override fun getLayoutId(): Int = R.layout.activity_shipping_address

    override fun initPresenter(): ShippingAddressPresent = ShippingAddressPresent()

    override fun initViews(savedInstanceState: Bundle?) {
        titleTv.text = getString(R.string.shippingAddress)
        rightTitleTv.text = getString(R.string.addNewAddress)

        springView.header = DefaultHeader(this)
        springView.footer = DefaultFooter(this)
        springView.setGive(SpringView.Give.BOTH)
        springView.type = SpringView.Type.FOLLOW
        springView.setListener(this)
        recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
//        recyclerView?.isItemViewSwipeEnabled = true
        var addressListAdapter =
            AdapterManager.getAddressListAdapter(this, data)

        val swipeMenuCreator =
            SwipeMenuCreator { swipeLeftMenu, swipeRightMenu, viewType ->
                val deleteItem = SwipeMenuItem(this)
                deleteItem.setText(this.getString(R.string.delete))
                    .setBackgroundColor(resources.getColor(R.color.red))
                    .setTextColor(Color.WHITE) // 文字颜色。
                    .setTextSize(15) // 文字大小。
                    .setWidth(240)
                    .setHeight(DensityUtil.dip2px(this, 120f))
                swipeRightMenu.addMenuItem(deleteItem)
            }
        // 设置监听器。
        // 设置监听器。
        recyclerView.setSwipeMenuCreator(swipeMenuCreator)
        val swipeMenuItemClickListener =
            SwipeMenuItemClickListener { menuBridge ->
                menuBridge.closeMenu()
                val menuPosition = menuBridge.adapterPosition
                showProgress()
                presenter.deleteAddress(data.get(menuPosition).useraddress_id)

            }
        recyclerView.setSwipeMenuItemClickListener(swipeMenuItemClickListener)

        var itemDecoration = MyDecoration()
        itemDecoration.setDividerHeight(dp2px(10))

        recyclerView.addItemDecoration(itemDecoration)

        recyclerView.adapter = addressListAdapter
        addressListAdapter.setOnItemClickListener(this)
    }

    fun dp2px(dpValue: Int): Float {
        return resources.displayMetrics.density * dpValue + 0.5f
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(top).statusBarDarkFont(false, 1.0f).init()
    }

    override fun initData() {
        data.clear()
        pageNo = 1
        presenter.getAddressList()

    }

    override fun initEvent() {
        rightTitleTv.setOnClickListener {
            startActivity(
                Intent(
                    this,
                    AddAddressActivity::class.java
                )
            )
        }
        backIv.setOnClickListener { finish() }
    }

    override fun getOrderId(): String = intent.getStringExtra("ordercommodityid")
    override fun updateSuccess() {
        hideProgress()
        EventBus.getDefault().post("refreshData")
        finish()
    }

    override fun getAddressId(): String = selectAddressId
    override fun updateFailure(msg: String?) {
        hideProgress()
        toast(msg)
    }

    override fun getAddressListSuccess(addressListBean: AddressListBean?) {//
        hideProgress()
        springView.onFinishFreshAndLoad()
        addressListBean?.addresslist?.let {
            if (pageNo == 1) {
                data.clear()
                if (it == null || it.size == 0) {
                    noDataIv.visibility = VISIBLE
                } else {
                    noDataIv.visibility = GONE
                }
            }
            data.addAll(it)
        }
        recyclerView.adapter?.notifyDataSetChanged()

    }

    override fun getAddressListFailure(msg: String?) {
        hideProgress()
        springView.onFinishFreshAndLoad()
        toast(msg)
    }

    override fun deleteSuccess() {
        hideProgress()
        springView.callFresh()
    }

    override fun deleteFailure(msg: String?) {
        hideProgress()
        toast(msg)
    }

    override fun getPage(): String = pageNo.toString()

    override fun onItemLongClick(
        view: View?,
        holder: RecyclerView.ViewHolder?,
        position: Int
    ): Boolean {
        return true
    }

    override fun onItemClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int) {
        var ordercommodityid = intent.getStringExtra("ordercommodityid")
        if (ordercommodityid != null) {
            showProgress()
            selectAddressId = data[position].useraddress_id
            presenter.updateAddress()
        } else {
            var addresslistBean = data[position]
            EventBus.getDefault().post(addresslistBean)
            finish()
        }
    }

    override fun onLoadmore() {
        pageNo += 1
        presenter.getAddressList()
    }

    override fun onRefresh() {
        pageNo = 1
        presenter.getAddressList()
    }
}
